

package com.web.my12306.service.impl;

import com.web.my12306.dao.IOrderDao;
import com.web.my12306.dao.ISeatDao;
import com.web.my12306.dao.ITrainDao;
import com.web.my12306.dao.impl.OrderDaoImpl;
import com.web.my12306.dao.impl.SeatDaoImpl;
import com.web.my12306.dao.impl.TrainDaoImpl;
import com.web.my12306.pojo.Orders;
import com.web.my12306.pojo.Seat;
import com.web.my12306.pojo.Train;
import com.web.my12306.service.OrderService;

import java.util.Calendar;
import java.util.List;
import java.util.Random;

public class OrderServiceImpl implements OrderService
{
    IOrderDao orderDao=new OrderDaoImpl();
    ISeatDao seatDao=new SeatDaoImpl();
    ITrainDao trainDao=new TrainDaoImpl();

    @Override
    /***
     * 黄逸
     ***/
    public Orders queryByOid(int oid) {
        return orderDao.queryByOid(oid);
    }

    @Override
    public List<Orders> getAll(int uid) {
        return  orderDao.getAll(uid);
    }

    @Override
    public List<Orders> getByUserPage(int uid, int index, int num)
    {
        return orderDao.getByUserPage(uid,index,num);
    }

    @Override
    public boolean cancelOrder(int oid)
    {
        orderDao.editStateByOid(oid,3);
        int sid=orderDao.queryByOid(oid).getSid();
        seatDao.editState(sid,1);
        return true;
    }

    @Override
    public boolean pay(int oid)
    {
        orderDao.editStateByOid(oid,2);
        return true;
    }

    @Override
    public Orders print(int oid)
    {
        if( orderDao.editStateByOid(oid,4))
        {
            return orderDao.queryByOid(oid);
        }
        return null;
    }

    @Override
    public void add(int uid, Seat seat)
    {
        Train train=trainDao.queryByTid(seat.getTid());
        Orders order = new Orders();
        Random rd = new Random();
        Calendar time = Calendar.getInstance();
        String onumber = String.valueOf(time.get(Calendar.YEAR)) + String.valueOf(time.get(Calendar.MONTH)) + String
                .valueOf(time.get(Calendar.DAY_OF_MONTH) + 1) + String.valueOf(time.get(Calendar.HOUR_OF_DAY)) +
                String.valueOf(time.get(Calendar.MINUTE)) + String.valueOf(time.get(Calendar.SECOND)) + String
                .valueOf(rd.nextInt(100));
        order.setOnumber(onumber);
        order.setUid(uid);
        order.setTrainno(train.getTrainno());
        order.setDeparture_time(train.getDeparture_Time());
        order.setArrival_time(train.getArrival_Time());
        order.setDstation(train.getDstation());
        order.setTerminal(train.getTerminal());
        order.setPrice(train.getPrice());
        order.setCarriage(seat.getCarriage());
        order.setSeatno(seat.getSeatno());
        order.setState(1);
        order.setSid(seat.getSid());
        order.setTid(train.getTid());
        orderDao.add(order);
        seatDao.editState(seat.getSid(),2);
    }
}
